#include <linux/err.h>
#include <linux/errno.h>
#include <linux/fs2.h>
#include <linux/pagemap.h>
#include <linux/highmem.h>
#include <linux/limits.h>

static int simple_delete_dentry(struct dentry *dentry)
{
	printf("this is %s(): %d\r\n", __func__, __LINE__);
	return 1;
}

struct dentry *simple_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd)
{
	static const struct dentry_operations simple_dentry_operations = {
		.d_delete = simple_delete_dentry,
	};

	printf("this is %s(): %d\r\n", __func__, __LINE__);

	if (dentry->d_name.len > NAME_MAX)
		return ERR_PTR(-ENAMETOOLONG);
	dentry->d_op = &simple_dentry_operations;
	d_add(dentry, NULL);
	printf("this is %s(): %d\r\n", __func__, __LINE__);
	return NULL;
}

int dcache_dir_open(struct inode *inode, struct file *file)
{
	static struct qstr cursor_name = {.len = 1, .name = "."};

	printf("this is %s(): %d\r\n", __func__, __LINE__);

	file->private_data = d_alloc(file->f_path.dentry, &cursor_name);

	printf("this is %s(): %d\r\n", __func__, __LINE__);
	return file->private_data ? 0 : -ENOMEM;
}

static inline unsigned char dt_type(struct inode *inode)
{
	return (inode->i_mode >> 12) & 15;
}

int dcache_readdir(struct file * filp, void * dirent, filldir_t filldir)
{
	struct dentry *dentry = filp->f_path.dentry;
	struct dentry *cursor = filp->private_data;
	struct list_head *p, *q = &cursor->d_u.d_child;
	ino_t ino;
	int i = filp->f_pos;

	printf("this is %s(): %d\r\n", __func__, __LINE__);

	switch (i) {
		// case 0:
		// 	printf("this is %s(): %d\r\n", __func__, __LINE__);
		// 	ino = dentry->d_inode->i_ino;
		// 	if (filldir(dirent, ".", 1, i, ino, DT_DIR) < 0)
		// 		break;
		// 	filp->f_pos++;
		// 	i++;
		// 	/* fallthrough */
		// case 1:
		// 	printf("this is %s(): %d\r\n", __func__, __LINE__);
		// 	ino = parent_ino(dentry);
		// 	if (filldir(dirent, "..", 2, i, ino, DT_DIR) < 0)
		// 		break;
		// 	filp->f_pos++;
		// 	i++;
		// 	/* fallthrough */
		default:
			printf("this is %s(): %d\r\n", __func__, __LINE__);
			// spin_lock(&dcache_lock);
			if (filp->f_pos == 2)
				list_move(q, &dentry->d_subdirs);

			for (p=q->next; p != &dentry->d_subdirs; p=p->next) {
				struct dentry *next;
				next = list_entry(p, struct dentry, d_u.d_child);
				if (d_unhashed(next) || !next->d_inode)
					continue;

#if 0
				spin_unlock(&dcache_lock);
				if (filldir(dirent, next->d_name.name, 
					    next->d_name.len, filp->f_pos, 
					    next->d_inode->i_ino, 
					    dt_type(next->d_inode)) < 0)
					return 0;
				spin_lock(&dcache_lock);
#else
				printf("this is %s(): %d >>> next->d_name.name = %s\r\n", __func__, __LINE__, next->d_name.name);
				strcpy(((struct dirents *)dirent)->dirent->d_name, next->d_name.name);
				((struct dirents *)dirent)->num++;
				((struct dirents *)dirent)->dirent++;
#endif

				/* next is still alive */
				list_move(q, p);
				p = q;
				filp->f_pos++;
			}
			// spin_unlock(&dcache_lock);
	}
	printf("this is %s(): %d\r\n", __func__, __LINE__);
	return 0;
}

ssize_t generic_read_dir(struct file *filp, char *buf, size_t siz, loff_t *ppos)
{
	printf("this is %s(): %d\r\n", __func__, __LINE__);
	return -EISDIR;
}

const struct file_operations simple_dir_operations = {
	.open		= dcache_dir_open,
	// .release	= dcache_dir_close,
	// .llseek		= dcache_dir_lseek,
	.read		= generic_read_dir,
	.readdir	= dcache_readdir,
	// .fsync		= simple_sync_file,
};

int simple_readpage(struct file *file, struct page *page)
{
	printf("this is %s(): %d\r\n", __func__, __LINE__);
	// clear_highpage(page);
	// flush_dcache_page(page);
	// SetPageUptodate(page);
	// unlock_page(page);
	return 0;
}

int simple_prepare_write(struct file *file, struct page *page,
			unsigned from, unsigned to)
{
	printf("this is %s(): %d\r\n", __func__, __LINE__);
	// if (!PageUptodate(page)) {
		if (to - from != PAGE_CACHE_SIZE)
			zero_user_segments(page,
				0, from,
				to, PAGE_CACHE_SIZE);
	// }
	printf("this is %s(): %d\r\n", __func__, __LINE__);
	return 0;
}

int simple_write_begin(struct file *file, struct address_space *mapping,
			loff_t pos, unsigned len, unsigned flags,
			struct page **pagep, void **fsdata)
{
	struct page *page;
	pgoff_t index;
	unsigned from;

	printf("this is %s(): %d\r\n", __func__, __LINE__);

	index = pos >> PAGE_CACHE_SHIFT;
	from = pos & (PAGE_CACHE_SIZE - 1);

	page = grab_cache_page_write_begin(mapping, index, flags);
	if (!page)
		return -ENOMEM;

	*pagep = page;

	printf("this is %s(): %d\r\n", __func__, __LINE__);
	return simple_prepare_write(file, page, from, from+len);
}

static int simple_commit_write(struct file *file, struct page *page,
			       unsigned from, unsigned to)
{
	struct inode *inode = page->mapping->host;
	loff_t pos = ((loff_t)page->index << PAGE_CACHE_SHIFT) + to;

	printf("this is %s(): %d\r\n", __func__, __LINE__);

	// if (!PageUptodate(page))
	// 	SetPageUptodate(page);
	/*
	 * No need to use i_size_read() here, the i_size
	 * cannot change under us because we hold the i_mutex.
	 */
	// if (pos > inode->i_size)
	// 	i_size_write(inode, pos);
	// set_page_dirty(page);
	return 0;
}

int simple_write_end(struct file *file, struct address_space *mapping,
			loff_t pos, unsigned len, unsigned copied,
			struct page *page, void *fsdata)
{
	unsigned from = pos & (PAGE_CACHE_SIZE - 1);

	printf("this is %s(): %d\r\n", __func__, __LINE__);

	/* zero the stale part of the page if we did a short copy */
	if (copied < len) {
		printf("this is %s(): %d\r\n", __func__, __LINE__);
		// void *kaddr = kmap_atomic(page, KM_USER0);
		// memset(kaddr + from + copied, 0, len - copied);
		// flush_dcache_page(page);
		// kunmap_atomic(kaddr, KM_USER0);
	}

	simple_commit_write(file, page, from, from+copied);

	// unlock_page(page);
	// page_cache_release(page);

	printf("this is %s(): %d\r\n", __func__, __LINE__);
	return copied;
}
